<?php

class Ead_ConteudoController extends App_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        
        $this->view->url_return = $this->_urlbase.'/'.$this->_modulo.'/curso';
        $this->view->a_title = 'Voltar aos cursos';
        
        $cur_id = $this->_getParam('cur_id', 0);
        
        if($cur_id == 0) $this->_helper->redirector('index','curso',$this->modulo,array());
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_list.js');
        
        $search = $this->_getParam('inp-search', '');
        $this->view->search = $search;
        $where = 'CON_STA = "1" AND CUR_ID = '.$cur_id;
        if(strlen($search) > 1){
            $where .= ' AND CON_NOM like "%'.$search.'%"';
        }
               
        $obj = new Ead_Model_DbTable_Conteudo();
        $result = $obj->fetchAll($obj->select()->where($where)->order(array('CON_ORD ASC','CON_NOM ASC')));
        $this->view->result = $this->paginacao($result);
        
        $this->view->cur_id = $cur_id;
        
    }
    
    
    public function editAction() {
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        $cur_id = $this->_getParam('cur_id', 0);
        
        $form = new Ead_Form_Conteudo();
        $form->submit->setLabel('Gravar');
        
        $form->CUR_ID->setValue($cur_id);
        
        $url = $this->_urlbase.'/'.$this->_modulo.'/'.$this->_controller.'/index/cur_id/'.$cur_id;
        $form->Cancelar->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($form->isValid($formData)) {
                    $id = (int) $form->getValue('CON_ID');

                    $data = $form->getValues();
                    unset($data['REP_NOM']);
                    unset($data['img']);
                    
                    $obj = new Ead_Model_DbTable_Conteudo();

                    if ($id == 0)
                        $obj->insert($data);
                    else
                        $obj->update($data, 'CON_ID = ' . $id);

                    $urlOptions = array('controller'=>$this->_controller,'action'=>'index','cur_id'=>$cur_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                } else {
                    $form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $form->populate($formData);
            }  
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Conteudo();
                $row = $obj->fetchRow('CON_ID = ' . $id);//->toArray();
                
                $rep = $row->findDependentRowSet("Ead_Model_DbTable_Repositorio"); 
                $row = $row->toArray();
                $row['REP_NOM'] = $rep[0]['REP_NOM'];
                
                $form->populate($row);
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Conteudo();
            //$ok = (bool) $obj->delete('CON_ID = '.(int)$id);
            $data = array('CON_STA' => 0);
            $ok = (bool) $obj->update($data, 'CON_ID = ' . $id);

        }
        
        echo json_encode(array('ok' => $ok));
    }
    
}

